
import query from '@dfeidao/atom-nodejs/db/query';
import { systemid, productid } from '../atom/config';

interface IBaseTable {
	_id: string;
}

interface Message {
	superior_category_number?: string;
	superior_category_number1?: string[];
}
interface Data extends IBaseTable {
	category_num: string;
	category_name: string;
	level: number;
	superior_category_number: string;
}
interface Data1 extends IBaseTable {
	category_num: string;
	category_name: string;
	level: number;
	superior_category_number: string;
}
//  superior_category_number 上级类别编号

export default async function atom(msg: Message, action_id: string, session_id: string) {
	const asc = ['fzno'];
	let superior_category_number = '';
	let superior_category_number1 = new Array();
	const { exec, prepare } = query(action_id, session_id, systemid);
	prepare('goods_category', ['category_num', 'category_name', 'level', 'superior_category_number'], { level: 1, productid }, 200, 1, asc, []);
	const [data] = await exec<Data>();
	if (msg.superior_category_number) {
		superior_category_number = msg.superior_category_number;
	} else {
		superior_category_number = data[0].category_num;
	}
	prepare('goods_category', ['category_num', 'category_name', 'level', 'superior_category_number'], { level: 2, superior_category_number, productid }, 200, 1, asc, []);
	const [data1] = await exec<Data1>();

	if (msg.superior_category_number1) {
		superior_category_number1 = msg.superior_category_number1;
	} else {
		superior_category_number1 = Array.from(data1).map((v) => {
			return v.category_num;
		});
	}

	prepare('goods_category', ['category_num', 'category_name', 'level', 'superior_category_number', 'pic_thumb_url'], { level: 3, superior_category_number: { $in: superior_category_number1 }, productid }, 200, 1, asc, []);
	const [data2] = await exec();

	return { data: { data, data1, data2 } };

}
